<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('user','',TRUE);
	}
 
	function index(){
	
		$this->load->library('form_validation');// Para ni mag chck sa credentials sa user.
		$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
   
		   if($this->form_validation->run() == FALSE){//if mali iyang credentials, back to login_view
		   		$data['content'] = 'login_view';
			 	$this->load->view('include/template', $data);
		   }
		   else{
			 redirect('home', 'refresh');//else tama iyang credentials proceed sa homepage
		   }
	}
	function check_database($password){
		$username = $this->input->post('username');//pag ang validation succeeded.  Validate against database
		$result = $this->user->login($username, $password);//query the database

		if($result){
			$sess_array = array();
			foreach($result as $row){
				$sess_array = array('user_id' => $row->user_id,
									'username' => $row->username
				);
			$this->session->set_userdata('logged_in', $sess_array);
			}
			return TRUE;
		}
		else{
			$this->form_validation->set_message('check_database', 'Invalid username or password');
			return false;}
		}
}
?>